﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using MM.Common.IRemoteDataBase;
using System.ServiceModel.Channels;

namespace MM.Common.DBBase
{
    internal class RemoteTransaction : ClientBase<ITransactionService>, ITransactionService
    {
        // Methods
        internal RemoteTransaction()
        {
        }

        internal RemoteTransaction(string endpointConfigurationName): base(endpointConfigurationName)
        {
        }

        internal RemoteTransaction(Binding binding, EndpointAddress address): base(binding, address)
        {
        }

        protected string InterFaceErrorMessage(string msg)
        {
            return ("接口端错误: " + msg);
        }

        public bool RemoteDBExecuteProcedure(ref string error, string procedure_name, ref OraParm[] parm)
        {
            error = "";
            try
            {
                return base.Channel.RemoteDBExecuteProcedure(ref error, procedure_name, ref parm);
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return false;
            }
        }

        public int RemoteDBExecuteSql(ref string error, string command_text, OraParm[] parm, int nrow)
        {
            error = "";
            try
            {
                int num = base.Channel.RemoteDBExecuteSql(ref error, command_text, parm, nrow);
                if (num < 0)
                {
                    error = error + "\r\n " + command_text;
                }
                return num;
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return -1;
            }
        }

        public bool RemoteDBIsExistTransaction()
        {
            try
            {
                return base.Channel.RemoteDBIsExistTransaction();
            }
            catch
            {
                return false;
            }
        }

        public bool RemoteDBTransactionBegin(ref string error)
        {
            error = "";
            try
            {
                if (base.Channel.RemoteDBTransactionBegin(ref error))
                {
                    return true;
                }
                error = this.ServiceErrorMessage(error);
                return false;
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return false;
            }
        }

        [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.AfterCall)]
        public bool RemoteDBTransactionCommit(ref string error)
        {
            error = "";
            try
            {
                if (base.Channel.RemoteDBTransactionCommit(ref error))
                {
                    return true;
                }
                error = this.ServiceErrorMessage(error);
                return false;
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return false;
            }
        }

        [OperationBehavior(ReleaseInstanceMode = ReleaseInstanceMode.AfterCall)]
        public bool RemoteDBTransactionRollback(ref string error)
        {
            error = "";
            try
            {
                if (base.Channel.RemoteDBTransactionRollback(ref error))
                {
                    return true;
                }
                error = this.ServiceErrorMessage(error);
                return false;
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return false;
            }
        }

        public bool RemoteSetRecordParameter(string table_order, int parm_len, long transaction_id)
        {
            try
            {
                return base.Channel.RemoteSetRecordParameter(table_order, parm_len, transaction_id);
            }
            catch
            {
                return false;
            }
        }

        public bool RemoteSetRegisterCode(ref string error, string rcode)
        {
            error = "";
            try
            {
                if (base.Channel.RemoteSetRegisterCode(ref error, rcode))
                {
                    return true;
                }
                error = this.ServiceErrorMessage(error);
                return false;
            }
            catch (Exception exception)
            {
                error = this.InterFaceErrorMessage(exception.Message);
                return false;
            }
        }

        protected string ServiceErrorMessage(string msg)
        {
            return ("服务端错误: " + msg);
        }
    }
}
